import API from '../../utils/api.js'
const App = getApp();
Page({ 

  /**
   * 页面的初始数据
   */
  data: {
    toppostion: 0,
    doctorHeight: 0,
    moveHeight: 0,
    topLooks: '20',
    animation: 1,
    showhideen: false,
    navList: ['医生', '配种', '丢失', '领养', '圈子', '问题'],
    num: 0,
    left: '4%',
    list: [{}, {}, {}],
    scroll_height: 0,
    scroll_height1: 0,
    doctoHeight: 0,
    questionList: [],
    showModal: false,
    //搜索
    inputShowed: false,
    inputVal: "",
    lat: '',
    lng: '',
    //配种地图
    markers1: [

    ],
    polyline1: [],
    //地图
    markers: [],
    polygon: [],
    polyline: [],
    controls: [{
      id: 1,
      //iconPath: '/resources/location.png',
      position: {
        // left: 0,
        // top: 300 - 50,
        // width: 50,
        // height: 50
      },
      clickable: true,
      circleList: []
    }],
    markers2: [],
    lng2: '',
    lat2: '',
    doctorList: [{}, {}, {}, {}]

  },
  scroll (e) {
    console.log(e.detail.scrollTop)
    var top = e.detail.scrollTop
    let windowHeight = wx.getSystemInfoSync().windowHeight;
    let windowWidth = wx.getSystemInfoSync().windowWidth;
    if (top >300) {
      this.setData({
        topLooks: 0,
        toppostion: 1,
        moveHeight: windowHeight * 750 / windowWidth - 180,
        doctorHeight: 1
      })
    }
  },
  onPageScroll: function (e) {
    // if (e.scrollTop > this.data.scrollTop) {
    //   //向下滚动 
    //   console.log('向下 ')
    //   let windowHeight = wx.getSystemInfoSync().windowHeight; // 屏幕的高度
    //   let windowWidth = wx.getSystemInfoSync().windowWidth; // 屏幕的宽度
    //   var self = this;
    //   self.setData({
    //     topLooks: 0,
    //     toppostion: 1,
    //     moveHeight: windowHeight * 750 / windowWidth - 180,
    //     doctorHeight: 1,
    //   })
    // } else {
      //向上滚动 
      // console.log('向上滚动 ')
      // let windowHeight = wx.getSystemInfoSync().windowHeight; // 屏幕的高度
      // let windowWidth = wx.getSystemInfoSync().windowWidth; // 屏幕的宽度
      // var self = this;
      // self.setData({
      //   topLooks: 0,
      //   toppostion: 0,
      //   moveHeight: windowHeight * 750 / windowWidth - 180,
      //   doctorHeight: 0
      // })
    //}
    //给scrollTop重新赋值 
    // this.setData({
    //   scrollTop: e.scrollTop
    // })
  //},
  // imagetouchchend: function() {
  //   let windowHeight = wx.getSystemInfoSync().windowHeight; // 屏幕的高度
  //   let windowWidth = wx.getSystemInfoSync().windowWidth; // 屏幕的宽度
  //   var self = this;
  //   self.setData({
  //     topLooks: 0,
  //     toppostion: 0,
  //     moveHeight: windowHeight * 750 / windowWidth - 200,
  //     doctorHeight: 0,
  //   })
  // },
  //拖动到顶部
  // imagetouchmove: function(e) {
  //   let windowHeight = wx.getSystemInfoSync().windowHeight; // 屏幕的高度
  //   let windowWidth = wx.getSystemInfoSync().windowWidth; // 屏幕的宽度
  //   var self = this;
  //   self.setData({
  //     topLooks: 0,
  //     toppostion: 1,
  //     moveHeight: windowHeight * 750 / windowWidth - 200,
  //     doctorHeight: 1
  //   })
  },
  onLoad: function(options) {
    var that = this;
    setTimeout(function() {
      that.setData({
        animation: 2
      })
    }, 2000)
    let windowHeight = wx.getSystemInfoSync().windowHeight // 屏幕的高度
    let windowWidth = wx.getSystemInfoSync().windowWidth // 屏幕的宽度
    console.log('lat', wx.getStorageSync('lat'))
    console.log('lng', wx.getStorageSync('lon'))
    var lat = wx.getStorageSync('lat')
    var lng = wx.getStorageSync('lon')
    var myaddress = wx.getStorageSync('address')
    this.questionList(1, 1)
    this.questionList(1, 3)
    this.setData({
      scroll_height: windowHeight * 750 / windowWidth - 220 - 130,
      scroll_height1: windowHeight * 750 / windowWidth - 650,
      doctoHeight: windowHeight * 750 / windowWidth - 630,
      doctoHeight1: windowHeight * 750 / windowWidth - 847 - 85,
      lat: lat,
      lng: lng,
      lat2: lat,
      lng2: lng,
      myaddress: myaddress,
      markers1: [{
        iconPath: "/img/dinwei.png",
        title: "我的位置",
        color: "#eeeeee",
        id: 0,
        latitude: lat,
        longitude: lng,
        width: 12,
        height: 24
      }],
      markers2: [{
        iconPath: "/img/dinwei.png",
        title: "我的位置",
        color: "#eeeeee",
        id: 0,
        latitude: lat,
        longitude: lng,
        width: 12,
        height: 24
      }],
      polygon: [{
        points: [{
          latitude: lat,
          longitude: lng
        }]
      }],
      polyline: [{
        points: [{
          longitude: lat,
          latitude: lng
        }, {
          longitude: lat,
          latitude: lng
        }],
        color: "#FF0000DD",
        width: 2,
        dottedLine: true
      }],

    })
  },


  onShow: function() {

  },
  regionchangeDoctor(e) {
    if (e.type == 'end' && (e.causedBy == 'scale' || e.causedBy == 'drag')) {
      console.log('是否进入')
      this.setData({
        markers2: []
      })
      var that = this;
      this.mapCtx = wx.createMapContext("map2");
      this.mapCtx.getCenterLocation({
        type: 'gcj02',
        success: function(res) {
          console.log('是否进入222')
          console.log(res)
          var lat = res.latitude.toFixed(5)
          var lng = res.longitude.toFixed(5)
          that.getNearbyData(lat, lng, 'markers2')
          that.mapCtx.translateMarker({
            autoRotate: true,
            duration: 1000,
            markerId: 0,
            rotate: 30,
            destination: {
              latitude: lat,
              longitude: lng
            },
            animationEnd() {
              console.log('animation end')
            }
          })

        }
      })
    }
  },
  //地图
  regionchange(e) {
    if (e.type == 'end' && (e.causedBy == 'scale' || e.causedBy == 'drag')) {
      console.log('是否进入')
      this.setData({
        markers1: []
      })
      var that = this;
      this.mapCtx = wx.createMapContext("map");
      this.mapCtx.getCenterLocation({
        type: 'gcj02',
        success: function(res) {
          console.log('是否进入222')
          console.log(res)
          var lat = res.latitude
          var lng = res.longitude
          that.setData({
            lat: lat,
            lng: lng,
            markers: [{
              iconPath: "/img/dinwei.png",
              title: "我的位置",
              color: "#eeeeee",
              id: 0,
              latitude: lat,
              longitude: lng,
              width: 12,
              height: 24
            }],
            circles: [{
              latitude: res.latitude,
              longitude: res.longitude,
              color: '#FF0000DD',
              fillColor: '#d1edff88',
              radius: 3000, //定位点半径
              strokeWidth: 1
            }]
          })
          that.mapCtx.moveToLocation({
            success(ret) {
              console.log('我要移动了')
            }
          })
          that.mapCtx.translateMarker({
            autoRotate: true,
            duration: 1000,
            markerId: 0,
            rotate: 10,
            destination: {
              latitude: lat,
              longitude: lng
            },
            animationEnd() {
              console.log('animation end')
            }
          })

        }
      })
    }
  },
  markertap(e) {
    console.log(e.markerId)
  },
  controltap(e) {
    console.log(e.controlId)
  },
  // 获取圈子内容
  getCircleList() {
    var data = {
      limit: 10,
      page: 1,
      type: 2
    }
    API.getNearby(data).then(res => {
      console.log(res)
      console.log(res.data);
      if (res.ret == 0) {
        this.setData({
          circleList: res.data
        })
      }
    })
  },
  getNearbyData(lat, lng, obj) {
    console.log('是否进入6666')
    console.log(obj)
    console.log(lat)
    var data = {
      page: 1,
      limit: 10,
      type: 3,
      latitude: lat,
      longitude: lng
    }
    var resetObj = {}
    resetObj[obj] = []
    this.setData(resetObj)
    console.log('666')
    console.log(this.data.markers2)
    API.answer(data).then(res => {
      console.log(res)
      var resultData = res.data
      if (res.ret == 0 && res.data.length > 0) {
        var obj1 = [{
          iconPath: "/img/dinwei.png",
          title: "我的位置",
          color: "#eeeeee",
          id: 0,
          latitude: lat,
          longitude: lng,
          width: 12,
          height: 24
        }]
        for (var k in resultData) {
          obj1.push({

            iconPath: '/img/fujin.png',
            title: "",
            color: "#eeeeee",
            id: k,
            latitude: resultData[k].latitude,
            longitude: resultData[k].longitude,
            width: 14,
            height: 18

          })
        }
        var newObj = {
          circles: [{

            latitude: lat,

            longitude: lng,

            color: '#FF0000DD',

            fillColor: '#d1edff88',

            radius: 3000,

            strokeWidth: 1

          }]
        }
        newObj[obj] = obj1
        console.log(newObj)
        this.setData(
          newObj
        )
        console.log(this.data.markers2)
      }
    })
  },
  questionList(page, type) {
    var data = {
      page: page,
      limit: 10,
      type: type,
      latitude: wx.getStorageSync('lat'),
      longitude: wx.getStorageSync('lon')
    }
    API.answer(data).then(res => {
      console.log(res)
      var questionList = this.data.questionList
      if (res.ret == 0 && res.data.length > 0) {
        this.setData({
          questionList: questionList.concat(res.data),
          doctorList: res.data
        })
      } else {
        this.setData({
          noData: true
        })
      }
    })
  },
  doctorBtn(e) {
    var dataset = e.currentTarget.dataset
    this.setData({
      lat2: dataset.lat,
      lng: dataset.lng
    })
    this.getNearbyData(dataset.lat, dataset.lng, 'markers2')
  },
  // 搜索
  showInput: function() {
    this.setData({
      inputShowed: true
    });
  },
  hideInput: function() {
    this.setData({
      inputVal: "",
      inputShowed: false
    });
  },
  clearInput: function() {
    this.setData({
      inputVal: ""
    });
  },
  inputTyping: function(e) {
    this.setData({
      inputVal: e.detail.value
    });
  },
  /**
   * 生命周期函数--监听页面加载
   */
  navTab(e) {
    var index = e.currentTarget.dataset.index
    console.log(index);
    this.setData({
      circleList: []
      // questionList: []
    })
    var left = '4%'
    if (index == 0) {
      left = '4%'
    } else if (index == 1) {
      left = '21%'
    } else if (index == 2) {
      left = '37.5%'
    } else if (index == 3) {
      left = '54%'
    } else if (index == 4) {
      left = '70%'
      this.getCircleList()
    } else {
      left = '87%'
      this.questionList(1, 3)
    }
    this.setData({
      num: index,
      left: left
    })
  },
  toDetail(e) {

    console.log(e)
    wx.navigateTo({
      url: '/pages/questionDetail/questionDetal?id=' + e.currentTarget.dataset.id,
    })
  },
  onPullDownRefresh: function() {

  },
  onReachBottom: function() {

  },
})